
//更新一条机械数据
function  chanLabco ()
{
    var myReg = /^[\u4e00-\u9fa5]+$/;
    if(myReg.test($('input[name="first_party"]').val())){
        if(myReg.test($('input[name="second_party"]').val())){
            if(myReg.test($('input[name="lease_method" ]').val())){
                if(myReg.test($('input[name="address"]').val())){

                    id= $('input[name="id"]').val();
                    date = $('form').serialize();
                    updeAjax(id,date);

                }else{
                    var stp5 = document.getElementById('stp5');
                    stp5.style.color = 'red';
                    stp5.innerHTML = '格式输入不正确(中文)';
                    return false;
                }
            }else{
                var stp4 = document.getElementById('stp4');
                stp4.style.color = 'red';
                stp4.innerHTML = '格式输入不正确(中文)';
                return false;
            }
        }else{
            var stp3 = document.getElementById('stp3');
            stp3.style.color = 'red';
            stp3.innerHTML = '格式输入不正确(中文)';
            return false;
        }
    }else{
        var stp2 = document.getElementById('stp2');
        stp2.style.color = 'red';
        stp2.innerHTML = '格式输入不正确(中文)';
        return false;
    }

}
function updeAjax(id,date){

    $.ajax({
        type:'post',
        url: '/admin/Contract/editormachine/id/'+ id,
        dataType:'json',
        data:date,

        success: function (data) {

            console.log(data);

            if (data.status) {
                alert('成功');
            } else {
                alert('请填写修改内容');
            }
        },

        error: function () {
            alert('AJAX执行失败');
        }
    });
}

//删除一条机械数据
$('.del-btn').click(function(){

    var ac_id = $(this).attr('data-id');
    console.log(ac_id);

    if (confirm('你确定要 [ 删 除 ] 吗?')) {
        var obj = $(this).parents('tr');
        delAjax(ac_id, obj);
    }

});
function delAjax(id,obj) {
    $.ajax({
        type: 'delete',
        url: '/admin/Contract/delectMachine/id/'+id,
        dataType: 'json',
        success: function (data) {
            console.log(data);
            if (data.status) {
                obj.remove();
            } else {
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}
//添加一条机械数据
$('.add_mechine').click(function(){

    var myReg = /^[\u4e00-\u9fa5]+$/;
    if(myReg.test($('input[name="first_party"]').val())){
        if(myReg.test($('input[name="second_party"]').val())){
            if(myReg.test($('input[name="lease_method"]').val())){
                if(myReg.test($('input[name="address"]').val())){
                    data = $('form').serialize();
                    SaddAjax(data);
                } else{
                    var stp4 = document.getElementById('stp4');
                    stp4.style.color = 'red';
                    stp4.innerHTML = '格式不正确4(中文)';
                    return false;
                }
            }else{
                var stp3 = document.getElementById('stp3');
                stp3.style.color = 'red';
                stp3.innerHTML = '格式不正确3(中文)';
                return false;
            }
        }else{
            var stp2 = document.getElementById('stp2');
            stp2.style.color = 'red';
            stp2.innerHTML = '格式不正确2(中文)';
            return false;
        }
    }else{
        var stp1 = document.getElementById('stp1');
        stp1.style.color = 'red';
        stp1.innerHTML = '格式不正确1(中文)';
        return false;
    }
});

function SaddAjax(data){
    $.ajax({
        type:'post',
        url: '/admin/Contract/addmachine',
        dataType:'json',
        data:data,
        success: function (data) {
            if (data.status) {
                alert('成功');
                return false;
            } else {
                alert('失败');
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}
//
$('.del-machine').click(function(){
    var ac_id = $(this).attr('data-id');
    console.log(ac_id);
    if (confirm('你确定要 [ 删 除 ] 吗?')) {
        var obj = $(this).parents('tr');
        dellAjax(ac_id, obj);
    }
});

//删除材料合同
function dellAjax(id,obj) {
    $.ajax({
        type: 'delete',
        url: '/admin/Contract/delectMaterial/id/'+id,
        dataType: 'json',
        success: function (data) {
            console.log(data);
            if (data.status) {
                obj.remove();
            } else {
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}
//验证数据
function checkForm()
{
    var myReg = /^[\u4e00-\u9fa5]+$/;
    if (myReg.test($('input[name="employers"]').val())){
        if(myReg.test($('input[name="lab_name"]').val())){
            var period=$('input[name="contract_period"]').val();
            if(  (period > 1 && period < 36) ){

                id= $('input[name="id"]').val();
                edupAjax(id);
            }else{
                var stp4 = document.getElementById('stp4');
                stp4.style.color = 'red';
                stp4.innerHTML = '格式不正确(1-36)';
                return false;
            }
        }else{
            var stp3 = document.getElementById('stp3');
            stp3.style.color = 'red';
            stp3.innerHTML = '格式不正确(中文)';
            return false;
        }
    } else {
        var stp2 = document.getElementById('stp2');
        stp2.style.color = 'red';
        stp2.innerHTML = '格式输入不正确(中文)';
        return false;

    }
}

//更新一条劳务数据
function edupAjax(id){
    $.ajax({
        type:'post',
        url: '/admin/Contract/addLabcontract/id/'+ id,
        dataType:'json',
        data: {
            id:$('input[name="id"]').val(),
            employers:$('input[name="employers"]').val(),
            lab_name:$('input[name="lab_name"]').val(),
            sign_time:$('input[name="sign_time"]').val(),
            contract_period:$('input[name="contract_period"]').val()
        },

        success: function (data) {
            if (data.status) {
                alert('成功');
            } else {
                alert('失败');
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}

//删除一条劳务合同
$('.del-geterial').click(function(){
    var ac_id = $(this).attr('data-id');
    if (confirm('你确定要 [ 删 除 ] 吗?')) {
        var obj = $(this).parents('tr');
        deterialAjax(ac_id, obj);
    }
});
function deterialAjax(id,obj) {
    $.ajax({
        type: 'delete',
        url: '/admin/Contract/delectLabcontract/id/'+id,
        dataType: 'json',
        success: function (data) {

            if (data.status) {
                obj.remove();
            } else {
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}
//编辑一条材料合同数据

$('#add_Material').click(function(){
    var myReg = /^[\u4e00-\u9fa5]+$/;
    if(myReg.test($('input[name="purchasing_unit"]').val()))
    {
        if(myReg.test($('input[name="supplier"]').val())){
            if(myReg.test($('input[name="material_content"]').val())){
                id= $('input[name="id"]').val();
                upAjax(id);
            }else{
                var stp3= document.getElementById('stp3');
                stp3.style.color = 'red';
                stp3.innerHTML = '格式输入不正确(中文)';
                return false;
            }
        }else{
            var stp2= document.getElementById('stp2');
            stp2.style.color = 'red';
            stp2.innerHTML = '格式输入不正确(中文)';
            return false;
        }
    }else{
        var stp1 = document.getElementById('stp1');
        stp1.style.color = 'red';
        stp1.innerHTML = '格式输入不正确(中文)';
        return false;
    }

});

function upAjax(id){
    $.ajax({
        type:'post',
        url: '/admin/Contract/updateMaterial/id/'+ id,
        dataType:'json',
        data: {
            id:$('input[name="id"]').val(),
            project_name:$('input[name="project_name"]').val(),
            purchasing_unit:$('input[name="purchasing_unit"]').val(),
            supplier:$('input[name="supplier"]').val(),
            material_content:$('input[name="material_content"]').val(),
            contract_sum:$('input[name="contract_sum"]').val(),
            payment_method:$('input[name="payment_method"]').val(),
            time:$('input[name="time').val(),
            address:$('input[name="address"]').val()
        },
        success: function (data) {
            console.log(data);
            if (data.status) {
                alert('成功');
            } else {
                alert('失败');
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}
//增加一条材料合同数据
$('.add_Warterial').click(function(){
    var data = $('form').serialize();
    ItAjax(data);

});
function ItAjax(data){
    $.ajax({
        type:'post',
        url: '/admin/Contract/addMaterial',
        dataType:'json',
        data:data,

        success: function (data) {
            console.log(data);
            if (data.status) {
                alert('成功');
                return false;
            } else {
                alert('失败');
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}


$('.get-fied').click(function(){
    var ac_id = $(this).attr('data-id');
    // console.log(ac_id);
    findAjax(ac_id);
});
function findAjax(id) {
    $.ajax({
        type: 'delete',
        url: '/admin/Contract/labcontractdetails/id/'+id,
        dataType: 'json',
        success: function (data) {
            // console.log(data);
            $('.form-group #employers').val(data.employers);
            $('.form-group #lab_name').val(data.lab_name);
            $('.form-group #sign_time').val(data.sign_time);
            $('.form-group #contract_period').val(data.contract_period);
            if (data.status) {
                $('.form-group #employers').html(data.employers);
            } else {
                alert('失败');
            }
        },
        error: function () {
            alert('AJAX执行失败');
        }
    });
}


